package org.example.config;


import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;


@Configuration
public class SecurityConfiguration {



    @Bean
    public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(User.builder().username("admin").password("{noop}1111").build());
        return manager;
    }


    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        // 配置所有请求都需要认证
        http.authorizeHttpRequests(requests -> requests.anyRequest().authenticated());

        http.formLogin(form -> form.loginPage("/login").loginProcessingUrl("/login").permitAll());

        http.oauth2Login(oauth2 -> oauth2.loginPage("/login"));



                //表单登录
//                .formLogin(Customizer.withDefaults())
            //开启OAuth2登录
//            .oauth2Login(Customizer.withDefaults());
        return http.build();
    }

}
